// 这个JS文件就是专门用于管理请求各种接口地址的
import Network from './network'

// 封装各种接口请求
export const getBanner = () => Network.get('banner?type=2'); //获取 banner( 轮播图 ) 数据
export const getPersonalized = ()=> Network.get('personalized?limit=6');//获取推荐歌单 limit: 取出数量 , 默认为 30
export const getNewAlbum = ()=> Network.get('album/newest');//获取云音乐首页新碟上架数据
export const getNewSong = () => Network.get('personalized/newsong');//获取推荐新音乐 limit: 取出数量 , 默认为 10
export const getPlayList = (data) => Network.get('playlist/detail', data);//获取歌单详情
export const getAlbum = (data) => Network.get('album', data);//获取专辑内容
export const getSongDetail = (data) => Network.get('song/detail', data);//获取歌曲详情
export const getSongLyric = (data) => Network.get('lyric', data);//传入音乐 id 可获得对应音乐的歌词
export const getSongURL = (data) => Network.get('song/url', data);//获取音乐 url
export const getArtistsSongs = (data) => Network.get('artists', data)//获取歌手歌曲列表
//获取热门歌手
export const getHotArtists = () => {
    return new Promise(function (resolve, reject) {
      Network.get('top/artists?offset=0&limit=5')
        .then(function (result) {
          resolve(result.artists)
        })
        .catch(function (err) {
          reject(err)
        })
    })
  }
//获取歌手分类(欧美/华语...)
//letter A-Z
export const getLetterArtists = (letter) => {
    return new Promise(function (resolve, reject) {
        let letterArtists = []
        Network.all([
        Network.get(`artist/list?type=-1&limit=5&area=7&initial=${letter}`),
        Network.get(`artist/list?type=-1&limit=5&area=96&initial=${letter}`),
        Network.get(`artist/list?type=-1&limit=5&area=8&initial=${letter}`),
        Network.get(`artist/list?type=-1&limit=5&area=16&initial=${letter}`),
        Network.get(`artist/list?type=-1&limit=5&area=0&initial=${letter}`),
        ])
        .then(function (result) {
            //console.log(result)
            result.forEach(function (item) {
              letterArtists.push(...item.artists)
            })
             //console.log(letterArtists)
            resolve([...new Set(letterArtists)])
        })
        .catch(function (err) {
            reject(err)
        })
    })
  }
  //将热门歌手和字母歌手组成数据
  //keys [热,A,B...] lise:[{歌手},{}]
  export const getAllArtists = (letter) => {
    return new Promise(function (resolve, reject) {
      let keys = ['热']
      let list = [getHotArtists()]
      for (let i = 65; i < 91; i++) {
        let char = String.fromCharCode(i)//获取ASCII A-Z
        keys.push(char)
        list.push(getLetterArtists(char))//获取promise对象
      }
      Network.all(list)
        .then(function (result) {
          let obj = {}
          obj.keys = keys
          obj.list = result
          resolve(obj)
        })
        .catch(function (err) {
          reject(err)
        })
    })
  }
  export const getTopListDetail = () => {
    return new Promise(function (resolve, reject) {
      // let category = {
      //   officialList: [
      //     { name: '云音乐飙升榜', id: 3 },
      //     { name: '云音乐新歌榜', id: 0 },
      //     { name: '网易原创歌曲榜', id: 2 },
      //     { name: '云音乐热歌榜', id: 1 }
      //   ],
      //   recList: [
      //     { name: '云音乐说唱榜', id: 23 },
      //     { name: '云音乐电音榜', id: 25 },
      //     { name: '云音乐欧美新歌榜', id: 32 },
      //     { name: '抖音排行榜', id: 26 },
      //     { name: '云音乐ACG音乐榜', id: 22 },
      //     { name: '云音乐古典音乐榜', id: 24 }
      //   ],
      //   globalList: [
      //     { name: '美国Billboard周榜', id: 6 },
      //     { name: 'UK排行榜周榜', id: 5 },
      //     { name: 'Beatport全球电子舞曲榜', id: 21 },
      //     { name: '日本Oricon周榜', id: 10 },
      //     { name: 'iTunes榜', id: 8 },
      //     { name: '英国Q杂志中文版周榜', id: 29 }
      //   ],
      //   otherList: [
      //     { name: 'KTV唛榜', id: 7 },
      //     { name: '法国 NRJ Vos Hits 周榜', id: 19 },
      //     { name: '新声榜', id: 27 },
      //     { name: '云音乐韩语榜', id: 28 },
      //     { name: '电竞音乐榜', id: 30 },
      //     { name: '云音乐欧美热歌榜', id: 31 }
      //   ],
      //   titles: { officialList: '官方榜', recList: '推荐榜', globalList: '全球榜', otherList: '更多榜单' }
      // }
      let category = {
        officialList: [
          { name: '飙升榜', id: 3 },
          { name: '新歌榜', id: 0 },
          { name: '原创榜', id: 2 },
          { name: '热歌榜', id: 1 }
        ],
        recList: [
          { name: '云音乐说唱榜', id: 23 },
          { name: '云音乐电音榜', id: 25 },
          { name: '云音乐古典榜', id: 32 },
          { name: '云音乐韩语榜', id: 26 },
          { name: '云音乐ACG榜', id: 22 },
          { name: '云音乐国电榜', id: 24 }
        ],
        globalList: [
          { name: '美国Billboard榜', id: 6 },
          { name: 'UK排行榜周榜', id: 5 },
          { name: 'Beatport全球电子舞曲榜', id: 21 },
          { name: '日本Oricon榜', id: 10 },
          { name: '俄语榜', id: 8 },
          { name: '法国 NRJ Vos Hits 周榜', id: 29 }
        ],
        otherList: [
          { name: 'KTV唛榜', id: 7 },
          { name: '网络热歌榜', id: 19 },
          { name: '云音乐ACG动画榜', id: 27 }, 
          { name: '云音乐ACG游戏榜', id: 28 },
          { name: '中国新乡村音乐排行榜', id: 30 },
          { name: '云音乐欧美热歌榜', id: 31 }
        ],
        titles: { officialList: '官方榜', recList: '推荐榜', globalList: '全球榜', otherList: '更多榜单' }
      }
      Network.get('toplist/detail')
        .then(function (data) {
          data.list.forEach(function (obj) {
            let flag = false
            for (let key in category) {
              for (let i = 0; i < category[key].length; i++) {
                if (category[key][i].name === obj.name) {
                  category[key][i].rank = obj
                  flag = true
                  break
                }
              }
              if (flag) {
                break
              }
            }
          })
          resolve(category)
        })
        .catch(function (err) {
          reject(err)
        })
    })
  }
  export const getTopList = (data) => Network.get('playlist/detail', data);//获取排行榜歌单
  export const getSearchList = (data) => Network.get('search?type=1', data);//获取搜索结果
  export const getSearchHot = () => Network.get('search/hot')//获取热门搜索
